package com.loveprogrammer.eternity.factory;

import com.alibaba.fastjson.JSONArray;
import com.loveprogrammer.eternity.enums.QualityEnum;
import com.loveprogrammer.eternity.enums.SkillQualityEnum;
import com.loveprogrammer.eternity.factory.template.MonsterTemplate;
import com.loveprogrammer.eternity.model.Monster;
import com.loveprogrammer.eternity.skill.Skill;
import com.loveprogrammer.eternity.skill.State;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @version 1.0.0
 * @description: 怪物工厂 产出怪物
 * @author: eric
 * @date: 2022-08-08 16:43
 **/
public class MonsterFactory {

    public static List<String> excludeSkills = Arrays.asList("81","82");

    private static List<MonsterTemplate> monsterTemplates;

    private static Map<Integer,MonsterTemplate> monsterTemplateMap;

    // 初始化野怪模版
    static {
        try {
            // 读取配置文件，然后加载到weaponTemplates中
            ClassLoader classLoader = EquipmentFactory.class.getClassLoader();
            InputStream inputStream = classLoader.getResourceAsStream("monster.json");

            assert inputStream != null;
            BufferedReader streamReader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
            StringBuilder responseStrBuilder = new StringBuilder();

            String inputStr;
            while ((inputStr = streamReader.readLine()) != null)
                responseStrBuilder.append(inputStr);

            monsterTemplates = JSONArray.parseArray(responseStrBuilder.toString(), MonsterTemplate.class);
            monsterTemplateMap = monsterTemplates.stream().collect(Collectors.toMap(MonsterTemplate::getId, e->e));

        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {
        for (MonsterTemplate monsterTemplate : monsterTemplates) {
            System.out.println(monsterTemplate.toString());
        }
    }

    public static Monster createMonster(int monsterId, int level, boolean checkExclude) {
        MonsterTemplate monsterTemplate = monsterTemplateMap.get(monsterId);
        QualityEnum qualityEnum = QualityEnum.白色;
        if(monsterTemplate.getQuality() > 5){
            qualityEnum = QualityEnum.暗金;
        }else if(monsterTemplate.getQuality() > 4){
            qualityEnum = QualityEnum.红色;
        }else if(monsterTemplate.getQuality() > 3){
            qualityEnum = QualityEnum.getById(new Random().nextInt(1)+4);
        }else{
            qualityEnum = QualityEnum.getById(new Random().nextInt(4)+1);
        }
        return createMonster(monsterTemplate,level,qualityEnum,checkExclude);
    }

    public static Monster createMonster(MonsterTemplate monsterTemplate, int level,
                                        QualityEnum qualityEnum,boolean checkExclude){
        Monster monster = new Monster(monsterTemplate, qualityEnum);
        String[] skills = monsterTemplate.getSkills().trim().split(",");
        String[] states = monsterTemplate.getStates().trim().split(",");
        String[] skillQualities = monsterTemplate.getSkillQuality().trim().split(",");
        monster.setLevel(level);
        for (int i = 0; i < skills.length; i++) {
            String skillId = skills[i];
            String skillQuality = skillQualities[i];
            // 有一些技能不能被继承
            if(checkExclude && excludeSkills.contains(skillId)){
                continue;
            }
            try {
                Skill skill = SkillFactory.getNewSkillById(skillId, SkillQualityEnum.getByCode(skillQuality));
                // TODO 这里要扩展一下
                if(skill != null) {
                    // 计算野怪的技能等级
                    skill.computeLevelByCharacterLevel(level);
                    monster.acquireSkill(skill);
                }
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }

        if(states.length > 0){
            List<State> stateList = new ArrayList<>();
            for (String state : states) {
                if("".equals(state)){
                    continue;
                }
                stateList.add(StateFactory.getById(Integer.parseInt(state)));
            }
            monster.setStates(stateList);
        }
        return monster.upLevel(level);
    }

    public static MonsterTemplate getById(int monsterId) {
        return monsterTemplateMap.get(monsterId);
    }
}
